package com.maiworld.seller.controller;


import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;
import java.util.UUID;



import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.dubbo.config.annotation.Reference;
import com.maiworld.content.service.ArticleService;
import com.maiworld.pojo.Category;
import com.maiworld.pojo.News;
import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;

import entity.PageResult;
import entity.Result;



@RestController
@RequestMapping("/article")
public class ArticleController {
	
	@Reference
	private ArticleService articleService;
	
	/**
	 * 查询文章分类 通过pid
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/findcategoryByPid",method =RequestMethod.GET)
	public List<Category> findCategotyById(Long pid){
		return articleService.findCategotyByPId(pid);		
	}
	/**
	 * 添加文章
	 * @param news
	 * @return
	 */
	@RequestMapping(value = "/addArticle",method =RequestMethod.POST)
	public Result addArticle(@RequestBody News news) {
		Result success = new Result(false,"添加失败");	

		news.setCreated(new Date());
//		System.out.println(news);
		int row = articleService.addArticle(news);
		if(row > 0) {
			success.setSuccess(true);
			success.setMessage("添加成功");
		}else {
			
		}
		return success;
	}
	/**
	 * 修改文章
	 * @param news
	 * @return
	 */
	@RequestMapping(value = "/saveArticle",method =RequestMethod.POST)
	public Result saveArticle(@RequestBody News news) {
		Result success = new Result(false,"添加失败");		 
//		System.out.println(news);
		int row = articleService.saveArticle(news);
		if(row > 0) {
			success.setSuccess(true);
			success.setMessage("添加成功");
		}else {
			
		}
		return success;
	}
	//批量删除
	@RequestMapping(value = "/deleteArticleByIdArray",method =RequestMethod.POST)
	public Result deleteArticleByIdArray(@RequestBody Long[] _idArray) {
		Result success = new Result(false,"添加失败");		 
//		System.out.println(_idArray);
		int row = articleService.deleteByIdArray(_idArray);
		if(row > 0) {
			success.setSuccess(true);
			success.setMessage("删除成功");
		}else {
			
		}
		return success;
	}
	
	/**
	 * 查询+分页
	 * @param 
	 * @param page
	 * @param rows
	 * @return
	 */
	@RequestMapping(value = "/search",method =RequestMethod.GET)
	public PageResult search(@RequestParam("classId") Long classId,@RequestParam("page") int page,@RequestParam("rows") int rows ,@RequestParam("status") String status ){
		return articleService.selectByClassId(classId, page, rows, status);		
	}
	
	/**
     * 多文件上传 图片上传
     * @return
	 * @throws IOException 
	 * @throws QiniuException 
     */
    @ResponseBody
    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public Map<String, Object> upload(@RequestParam("dropFile") MultipartFile[] files) throws QiniuException, IOException{
        Map<String, Object> result = new HashMap<>();
        ArrayList<String> arraylist = new ArrayList<String>();
        System.out.println(files.length);
		for (int i = 0; i < files.length; i++) {
			// 配置七牛云密钥
			String ACCESS_KEY = "Og486mvhR4p1jkfN2qCaTwmiFy4hc9fYZdyQj9QV";
			String SECRET_KEY = "RiQjmANE8shkcnjfLhqUV0g0G8tGyXdmGFG2irES";
			// 要上传的空间--
			String bucketname = "sanfeng";
			// 上传文件的路径
			String FilePath = "http://static.sanfeng.maiworld.net/";
			// 密钥配置
			Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
			// 创建上传对象
			UploadManager uploadManager = new UploadManager(new Configuration(Zone.zone0()));
			// 创建随机名字
			String originalFilename = files[i].getOriginalFilename();
			String mine = files[i].getContentType();
			System.out.println(mine);
			String newFileName = "photo/" + UUID.randomUUID()
					+ originalFilename.substring(originalFilename.lastIndexOf("."));
			// 调用put方法来进行上传
			Response res = uploadManager.put(files[i].getInputStream(), newFileName, auth.uploadToken(bucketname), null,
					null);
			// 打印返回信息,200为上传成功

			Ret ret = res.jsonToObject(Ret.class);
			System.out.println(ret.key);
			arraylist.add(FilePath + ret.key);

		}
		result.put("result", arraylist);
        return result;
    }
    
	/**
     * 单文件上传 视频上传
     * @return
	 * @throws IOException 
	 * @throws QiniuException 
     */
    @ResponseBody
    @RequestMapping(value = "/uploadVideo", method = RequestMethod.POST)
    public Map<String, Object> uploadVideo(@RequestParam("video") MultipartFile video) throws QiniuException, IOException{
        Map<String, Object> result = new HashMap<>();
        System.out.println(video.getOriginalFilename());       
        if(!video.isEmpty()) {
        	 //配置七牛云密钥
            String ACCESS_KEY = "Og486mvhR4p1jkfN2qCaTwmiFy4hc9fYZdyQj9QV";  
            String SECRET_KEY = "RiQjmANE8shkcnjfLhqUV0g0G8tGyXdmGFG2irES"; 
            //要上传的空间--  
            String bucketname = "sanfeng";
             //上传文件的路径  
            String FilePath ="http://static.sanfeng.maiworld.net/"; 
             //密钥配置  
             Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);  
            //创建上传对象  
            UploadManager uploadManager = new UploadManager(new Configuration(Zone.zone0()));  
             //创建随机名字
             String originalFilename=video.getOriginalFilename();
             String mine=video.getContentType();
             System.out.println(mine);
             String newFileName="video/"+UUID.randomUUID()+originalFilename.substring(originalFilename.lastIndexOf("."));
            //调用put方法来进行上传
             Response res = uploadManager.put(video.getInputStream(), newFileName, 
              auth.uploadToken(bucketname),null,null);
            //打印返回信息,200为上传成功
            
            Ret ret = res.jsonToObject(Ret.class);
            System.out.println(ret.key);
            result.put("videoResult",FilePath+ret.key);
        }
 
        return result;
    }
    
    /**
     * 单文件上传 视频上传
     * @return
	 * @throws IOException 
	 * @throws QiniuException 
     */
    @ResponseBody
    @RequestMapping(value = "/uptoken", method = RequestMethod.GET)
    public Map<String, Object> uptoken() throws QiniuException, IOException{
        Map<String, Object> result = new HashMap<>();
      
		// 配置七牛云密钥
		String ACCESS_KEY = "Og486mvhR4p1jkfN2qCaTwmiFy4hc9fYZdyQj9QV";
		String SECRET_KEY = "RiQjmANE8shkcnjfLhqUV0g0G8tGyXdmGFG2irES";
		// 要上传的空间--
		String bucketname = "sanfeng";
		// 上传文件的路径
		String FilePath = "http://static.sanfeng.maiworld.net/";
		// 密钥配置
		Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);

		result.put("uptoken", auth.uploadToken(bucketname));
 
        return result;
    }
    
    class Ret {
        public long fsize;
        public String key;
        public String hash;
        public int width;
        public int height;
    }
}
